package com.byb.server.service.impl;

import com.byb.server.service.AchievementService;
import com.byb.server.domain.Achievement;
import com.byb.server.repository.AchievementRepository;
import com.byb.server.repository.search.AchievementSearchRepository;
import com.byb.server.service.dto.AchievementDTO;

import com.byb.server.service.mapper.AchievementMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Achievement.
 */
@Service
@Transactional
public class AchievementServiceImpl implements AchievementService{

    private final Logger log = LoggerFactory.getLogger(AchievementServiceImpl.class);

    private final AchievementRepository achievementRepository;

    private final AchievementMapper achievementMapper;

    private final AchievementSearchRepository achievementSearchRepository;
    public AchievementServiceImpl(AchievementRepository achievementRepository, AchievementMapper achievementMapper, AchievementSearchRepository achievementSearchRepository) {
        this.achievementRepository = achievementRepository;
        this.achievementMapper = achievementMapper;
        this.achievementSearchRepository = achievementSearchRepository;
    }

    /**
     * Save a achievement.
     *
     * @param achievementDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public AchievementDTO save(AchievementDTO achievementDTO) {
        log.debug("Request to save Achievement : {}", achievementDTO);
        Achievement achievement = achievementMapper.toEntity(achievementDTO);
        achievement = achievementRepository.save(achievement);
        AchievementDTO result = achievementMapper.toDto(achievement);
       // achievementSearchRepository.save(achievement);
        return result;
    }

    /**
     *  Get all the achievements.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<AchievementDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Achievements");
        return achievementRepository.findAll(pageable)
            .map(achievementMapper::toDto);
    }

    /**
     *  Get one achievement by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public AchievementDTO findOne(Long id) {
        log.debug("Request to get Achievement : {}", id);
        Achievement achievement = achievementRepository.findOne(id);
        return achievementMapper.toDto(achievement);
    }

    /**
     *  Delete the  achievement by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Achievement : {}", id);
        achievementRepository.delete(id);
        //achievementSearchRepository.delete(id);
    }

    /**
     * Search for the achievement corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<AchievementDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of Achievements for query {}", query);
        Page<Achievement> result = achievementSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(achievementMapper::toDto);
    }

    /**
     *  Get one achievement by Product_id.
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public AchievementDTO findOneByProductId(Long id) {
        log.debug("Request to get Achievement By ProductId : {}", id);
        Achievement achievement = achievementRepository.findByProductId(id);
        return achievementMapper.toDto(achievement);
    }
}
